package org.hagopa.games.moneypool;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Configuration {
	private static Configuration Instance = null;
	private String pathToConfigXml = null;
	private static int serverId = 0;
	private static String pathToPolicyXml = null;
	private static boolean allowPoliceInsideServer = false;
	private static int port = 3456;
	private static String DBHost = "localhost";
	private static String user = "root";
	private static String pwd = "";
	private static String dbname = "";
        private static boolean enableDBLog = true;
	

	
	private String answer = "yes";
	public static String getPathToPolicyXml() {
		return pathToPolicyXml;
	}

	public static void setPathToPolicyXml(String pathToPolicyXml) {
		Configuration.pathToPolicyXml = pathToPolicyXml;
	}
        public static boolean DBLogEnableFlag()
        {
            return enableDBLog;
        }
	private Configuration()
	{
		this.pathToConfigXml = "configuration/SrvConfig.xml";
				
		readXmlFile();	
		
	}
	
	private void readXmlFile()
	{
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = null;
        Document doc = null;
        File xmlFile = null;
        xmlFile =new File(this.pathToConfigXml);
        if(xmlFile == null)
        {
        	this.pathToConfigXml = "../configuration/SrvConfig.xml";
        	xmlFile =new File(this.pathToConfigXml);
        }
		try {
			docBuilder = docBuilderFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
        try {
			
        	doc = docBuilder.parse(xmlFile);
			doc.getDocumentElement().normalize();
			System.out.println("\n===========================\n\tConfiguration:\n===========================\n");	
			NodeList genSettings = doc.getElementsByTagName("Config");
			for(int i = 0;i< genSettings.getLength(); i++)
			{
				Element element = (Element) genSettings.item(i);
				String name = element.getAttribute("name");
				
				if(name.equals("Server"))
				{
					NodeList srvCfg = element.getElementsByTagName("tag");
                                        serverId = Integer.parseInt(srvCfg.item(0).getTextContent());
					port = Integer.parseInt(srvCfg.item(1).getTextContent());
					pathToPolicyXml = srvCfg.item(2).getTextContent();
					if(answer.equals(srvCfg.item(3).getTextContent()))
					{
						allowPoliceInsideServer = true;
						System.out.println("allowing police inside server");
					}
                                        if(answer.equals(srvCfg.item(4).getTextContent()))
                                            enableDBLog = true;
                                        else
                                            enableDBLog = false;
					System.out.println("General Settings for Server (id)" + serverId +" \n-----------------\n Port =" + port + "\n PolicyFile path: " + pathToPolicyXml +"\n");
				}
				else if(name.equals("DB"))
				{
					
					System.out.println("\n" + name + "\n-----------------\n");
					NodeList settings = element.getChildNodes();
					
					DBHost = settings.item(1).getTextContent();
					System.out.println("\t" + ((Element)settings.item(1)).getAttribute("name")+": " + settings.item(1).getTextContent());
					
					user = settings.item(3).getTextContent();
					System.out.println("\t" + ((Element)settings.item(3)).getAttribute("name")+ ": " + settings.item(3).getTextContent());
					
					pwd = settings.item(5).getTextContent();
					System.out.println("\t" + ((Element)settings.item(5)).getAttribute("name") + ": " + settings.item(5).getTextContent());
					dbname = settings.item(7).getTextContent();
					System.out.println("\t" + ((Element)settings.item(7)).getAttribute("name") + ": " + settings.item(7).getTextContent());
					System.out.println("============================\n");
				}
				else 
					{
						continue;
					
					}
			}
			
			
			
			
		} catch (SAXException se) {
			// TODO Auto-generated catch block
			se.printStackTrace();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	private synchronized static void createInstance()
	{
		if(Instance == null)
		{
			Instance = new Configuration();
		}
	}
	public static Configuration getInstance()
	{
		if(Instance == null)
			createInstance();
		return Instance;
		
	}
	public static String getDBHost() {
		return DBHost;
	}
        public static int getServerID() {
		return serverId;
	}

	public static void setDBHost(String host) {
		DBHost = host;
	}

	public static String getUser() {
		return user;
	}

	

	public static String getPwd() {
		return pwd;
	}

	public void setPwd(String passwd) {
		pwd = passwd;
	}

	public static int getPort() {
		return port;
	}

	public void setPort(int portIn) {
		port = portIn;
	}

	public static String getDbname() {
		return dbname;
	}

	public static void setDbname(String dbname) {
		Configuration.dbname = dbname;
	}

	public static boolean isAllowPoliceInsideServer() {
		return allowPoliceInsideServer;
	}

	public static void setAllowPoliceInsideServer(boolean allowPoliceInsideServer) {
		Configuration.allowPoliceInsideServer = allowPoliceInsideServer;
	}
	
}
